package user

import (
	"context"
	"strconv"

	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/utils/pointy"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendGetUserByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 通过ID获取User
func NewBackendGetUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendGetUserByIdLogic {
	return &BackendGetUserByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BackendGetUserByIdLogic) BackendGetUserById(req *types.IDReq) (resp *types.GetUserByIdResp, err error) {
	data, err := l.svcCtx.UserRpc.BackendGetUserById(l.ctx, &userclient.IDReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}
	option := utils.GetOptionByStatus(data.Status)
	id, _ := strconv.Atoi(*data.Id)

	return &types.GetUserByIdResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.UserInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         pointy.GetPointer(uint64(id)),
				CreatedAt:  data.CreatedAt,
				UpdatedAt:  data.UpdatedAt,
				DeletedAt:  data.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			Username:         data.Username,
			Email:            data.Email,
			Mobile:           data.Mobile,
			Status:           data.Status,
			LastLoginTime:    data.LastLoginTime,
			EmailVerified:    data.EmailVerified,
			EmailVerifiedAt:  data.EmailVerifiedAt,
			MobileVerified:   data.MobileVerified,
			MobileVerifiedAt: data.MobileVerifiedAt,
			CreatedIP:        data.CreatedIP,
			IsSystem:         data.IsSystem,
		},
	}, nil
}
